package cn.t.repository.impl.device;

import cn.t.converter.device.DeviceConfigRowConverter;
import cn.t.core.repoistory.RepositoryImpl;
import cn.t.model.device.DeviceConfigRowDO;
import cn.t.persistence.entity.device.DeviceConfigRowPO;
import cn.t.persistence.mapper.device.DeviceConfigRowMapper;
import cn.t.repository.device.DeviceConfigRowRepository;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 配置行数据仓库
 */
@Slf4j
@Repository
@AllArgsConstructor
public class DeviceConfigRowRepositoryImpl extends RepositoryImpl<DeviceConfigRowMapper, DeviceConfigRowPO, DeviceConfigRowConverter, DeviceConfigRowDO> implements DeviceConfigRowRepository {


    @Override
    public List<DeviceConfigRowDO> findByModel(DeviceConfigRowDO deviceConfigRowDO, int start, Integer pageSize, String field, String direction) {

        String lastSql = StringUtils.hasText(field) ?
                " order by " + field + " " + direction + " limit " + start + "," + pageSize : " limit " + start + "," + pageSize;

        List<DeviceConfigRowPO> list = getWrapper(deviceConfigRowDO).last(lastSql).list();
        if (CollectionUtils.isEmpty(list)) {
            return Collections.emptyList();
        }

        return list.stream().map(converter::po2Model).collect(Collectors.toList());
    }

    private LambdaQueryChainWrapper<DeviceConfigRowPO> getWrapper(DeviceConfigRowDO deviceConfigRowDO) {
        LambdaQueryChainWrapper<DeviceConfigRowPO> wrapper = lambdaQuery();
        if (!ObjectUtils.isEmpty(deviceConfigRowDO.getConfigId())) {
            wrapper.eq(DeviceConfigRowPO::getConfigId, deviceConfigRowDO.getConfigId());
        }
        if (!ObjectUtils.isEmpty(deviceConfigRowDO.getDeviceId())) {
            wrapper.eq(DeviceConfigRowPO::getDeviceId, deviceConfigRowDO.getDeviceId());
        }
        if (!ObjectUtils.isEmpty(deviceConfigRowDO.getGmtModified())) {
            wrapper.between(DeviceConfigRowPO::getGmtModified, deviceConfigRowDO.getGmtModifiedStart(), deviceConfigRowDO.getGmtModifiedEnd());
        }
        if (!ObjectUtils.isEmpty(deviceConfigRowDO.getGmtCreated())) {
            wrapper.between(DeviceConfigRowPO::getGmtCreated, deviceConfigRowDO.getGmtCreatedStart(), deviceConfigRowDO.getGmtCreatedEnd());
        }
        return wrapper;
    }

    @Override
    public long count(DeviceConfigRowDO deviceConfigRowDO) {
        return getWrapper(deviceConfigRowDO).count();
    }

    @Override
    public boolean removeByConfigId(Long configId) {
        return this.remove(new LambdaQueryWrapper<DeviceConfigRowPO>().eq(DeviceConfigRowPO::getConfigId, configId));
    }

    @Override
    public boolean addBatch(List<DeviceConfigRowDO> deviceConfigRowDOS) {
        List<DeviceConfigRowPO> deviceConfigRowPOS = deviceConfigRowDOS.stream().map(this.converter::model2Po).collect(Collectors.toList());
        return this.saveBatch(deviceConfigRowPOS);
    }
}
